<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ActivityPrize;
use App\Models\ActivityPrize as ActivityPrizeModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\ActivityTable;
use App\Models\Activity;

class ActivityPrizeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = ActivityPrize::with('activity');
        return Grid::make($model, function (Grid $grid) {
            $grid->model()->orderBy('order');
            $grid->column('id')->sortable();
            $grid->column('activity.title', '所属活动')->label();
            $grid->column('type')->display(function ($type) {
                return ActivityPrizeModel::$typeMap[$type];
            })->label('default');
            $grid->column('level')->select(ActivityPrizeModel::$levelMap);
            $grid->column('name');
            $grid->column('image')->display(function ($banner) {
                return '<img src="' . $banner . '" width="100">';
            });
            $grid->column('probability')->editable(true);
            $grid->column('stock');
            $grid->column('order')->orderable();
            $grid->column('valid')->switch();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(2)->integer();
                $filter->like('name')->width(3);
                $filter->equal('activity_id', '活动')->width(3)
                    ->selectTable(ActivityTable::make(['id' => '']))
                    ->title('活动列表')
                    ->dialogWidth('50%')
                    ->model(Activity::class, 'id', 'title');
                $filter->equal('valid')->select([0 => '否', 1 => '是'])->width(2);
            });
            $grid->export()->csv();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $model = ActivityPrize::with('activity');

        return Show::make($id, $model, function (Show $show) {
            $show->field('id');
            $show->field('activity.title', '抽奖活动');
            $show->field('type')->unescape()->as(function ($type) use ($show) {

                return ActivityPrizeModel::$typeMap[$type];
            });
            $show->field('valid_at');
            $show->field('due_at');
            $show->field('level');
            $show->field('name');
            $show->field('description');
            $show->field('image')->image();
            $show->field('little_image')->image();
            $show->field('probability');
            $show->field('stock');
            $show->field('order');
            $show->field('valid')->using([0 => '否', 1 => '是']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ActivityPrize(), function (Form $form) {

            $form->tab('基础设置', function (Form $form) {
                $form->display('id');
                $form->selectTable('activity_id', '活动')
                    ->from(ActivityTable::make(['id' => $form->getKey()]))
                    ->title('活动列表')
                    ->dialogWidth('50%')
                    ->model(Activity::class, 'id', 'title');
                $form->select('level')->options(ActivityPrizeModel::$levelMap)->required();
                $form->text('name')->required();
                $form->editor('description')->required();
                $form->image('image')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
                $form->image('little_image')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
                // $form->number('order')->required();
                $form->switch('valid');
            })->tab('类型设置', function (Form $form) {

                $form->radio('type')
                    ->when(ActivityPrizeModel::TYPE_COUPON, function (Form $form) {
                        $form->radio('valid_at_type', '生效方式')
                            ->rules('required_if:type,' . ActivityPrizeModel::TYPE_COUPON)
                            ->setLabelClass(['asterisk'])
                            ->when('datetime', function (Form $form) {
                                $form->datetime('valid_at_datatime', '生效时间')->rules('required_if:valid_at_type,datetime')->setLabelClass(['asterisk']);
                            })
                            ->when('days', function (Form $form) {
                                $form->number('valid_at_days', '生效天数')->rules('required_if:valid_at_type,days')->setLabelClass(['asterisk']);
                            })
                            ->options([
                                'now' => '立即生效',
                                'datetime' => '指定时间',
                                'days' => 'N天后',
                            ])
                            ->default('now');

                        $form->radio('due_at_type', '失效方式')
                            ->rules('required_if:type,' . ActivityPrizeModel::TYPE_COUPON)
                            ->setLabelClass(['asterisk'])
                            ->when('datetime', function (Form $form) {
                                $form->datetime('due_at_datatime', '失效时间')->rules('required_if:due_at_type,datetime')->setLabelClass(['asterisk']);
                            })->when('days', function (Form $form) {
                                $form->number('due_at_days', '失效天数')->rules('required_if:due_at_type,days')->setLabelClass(['asterisk']);
                            })->when('months', function (Form $form) {
                                $form->number('due_at_months', '失效月数')->rules('required_if:due_at_type,months')->setLabelClass(['asterisk']);
                            })->when('years', function (Form $form) {
                                $form->number('due_at_years', '失效年数')->rules('required_if:due_at_type,years')->setLabelClass(['asterisk']);
                            })
                            ->options([
                                'datetime' => '指定时间',
                                'days' => 'N天后',
                                'months' => 'N月后',
                                'years' => 'N年后'
                            ])
                            ->default('years');
                    })
                    ->options(ActivityPrizeModel::$typeMap)
                    ->default(ActivityPrizeModel::TYPE_NORMAL);
            })->tab('库存设置', function (Form $form) {
                $form->rate('probability')->required();
                $form->number('stock', '当前库存')->required();
                $form->radio('limit')
                    ->when([ActivityPrizeModel::LIMIT_DAILY, ActivityPrizeModel::LIMIT_MONTHLY, ActivityPrizeModel::LIMIT_WEEKLY, ActivityPrizeModel::LIMIT_YEARLY], function (Form $form) {
                        $form->number('limit_value', '发放数')->rules('required_unless:limit,' . ActivityPrizeModel::LIMIT_TOTAL)->setLabelClass(['asterisk']);
                    })
                    ->options(ActivityPrizeModel::$limitMap)
                    ->default(ActivityPrizeModel::LIMIT_TOTAL);
            });
        });
    }
}
